package com.sanpu.kxe.receiver;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.KxeApplication;
import com.sanpu.kxe.common.utils.NoticeUtils;
import com.sanpu.kxe.jump.eventbus.RefreshUserEvent;
import com.sanpu.kxe.module.dialog.PushMsgAuthenDialog;
import com.sanpu.kxe.module.dialog.PushMsgTransactionDialog;
import com.sanpu.kxe.module.dialog.PushMsgUpgradeDialog;
import com.sanpu.kxe.module.dialog.PushNoticeDialog;
import com.tencent.android.tpush.XGPushBaseReceiver;
import com.tencent.android.tpush.XGPushClickedResult;
import com.tencent.android.tpush.XGPushRegisterResult;
import com.tencent.android.tpush.XGPushShowedResult;
import com.tencent.android.tpush.XGPushTextMessage;

import org.greenrobot.eventbus.EventBus;

/**
 * Description：消息推送
 * Created by 薛勇军 on 2018/7/9.
 */

public class MessageReceiver extends XGPushBaseReceiver {

    public MessageReceiver() {
    }

    @Override
    public void onRegisterResult(Context context, int i, XGPushRegisterResult xgPushRegisterResult) {

    }

    @Override
    public void onUnregisterResult(Context context, int i) {

    }

    @Override
    public void onSetTagResult(Context context, int i, String s) {

    }

    @Override
    public void onDeleteTagResult(Context context, int i, String s) {

    }

    @Override
    public void onTextMessage(Context context, XGPushTextMessage xgPushTextMessage) {
        String customContent = xgPushTextMessage.getCustomContent();
        if (customContent != null && customContent.length() != 0) {
            try {
                MessageOutput output = null;
                if (!TextUtils.isEmpty(customContent)) {
                    output = new Gson().fromJson(customContent, new TypeToken<MessageOutput>() {
                    }.getType());
                }
                if (output != null) {
                    switch (output.getPushMessageType()) {
                        case "TRANSACTION":
                            showTransactionView(output);
                            NoticeUtils.getInstance().sendPayNotification(output);
                            break;
                        case "RECHARGE_MEMBER":
                            showRechargeMemberView(output);
                            sendSystemNotification(output.getTitle(), output.getContent(), null);
                            break;
                        case "REBATE":
                            sendSystemNotification(output.getTitle(), output.getContent(), "MyRebTotalActivity");
                            break;
                        case "AUTH_IDENTITY":
                            showAuthIdentityView(output);
                            sendSystemNotification(output.getTitle(), output.getContent(), null);
                            break;
                        case "MOBILE_RECHARGE":
                            sendSystemNotification(output.getTitle(), output.getContent(), "TransactionActivity");
                            break;
                        case "VIP_EXPIRE":
                            NoticeUtils.getInstance().sendMemberNotification(output.getTitle(), output.getContent());
                            break;
                        case "WITHDRAW":
                            sendSystemNotification(output.getTitle(), output.getContent(), null);
                            break;
                        case "NOTICE":
                            sendSystemNotification(output.getTitle(), output.getContent(), null);
                            break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onNotifactionClickedResult(Context context, XGPushClickedResult xgPushClickedResult) {
        if (xgPushClickedResult.getActionType() == XGPushClickedResult.NOTIFACTION_CLICKED_TYPE) {
            // 通知在通知栏被点击啦。。。。。
            // APP自己处理点击的相关动作
            // 这个动作可以在activity的onResume也能监听，请看第3点相关内容
            String customContent = xgPushClickedResult.getCustomContent();
            if (customContent != null && customContent.length() != 0) {
                try {
                    MessageOutput output = null;
                    if (!TextUtils.isEmpty(customContent)) {
                        output = new Gson().fromJson(customContent, new TypeToken<MessageOutput>() {
                        }.getType());
                    }
                    if (output != null && TextUtils.equals("NOTICE", output.getPushMessageType())) {
                        showPushNoticeView(output);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (xgPushClickedResult.getActionType() == XGPushClickedResult.NOTIFACTION_DELETED_TYPE) {
            // 通知被清除啦。。。。
            // APP自己处理通知被清除后的相关动作
        }
    }

    @Override
    public void onNotifactionShowedResult(Context context, XGPushShowedResult xgPushShowedResult) {
        if (context == null || xgPushShowedResult == null) {
            return;
        }
    }

    /**
     * 状态栏通知
     *
     * @param target 跳转目标
     */
    public void sendSystemNotification(String title, String content, String target) {
        if (TextUtils.isEmpty(target)) {
            NoticeUtils.getInstance().sendSystemNotification(title, content);
        } else {
            NoticeUtils.getInstance().sendSystemNotification(title, content, target);
        }
    }

    /**
     * 交易消息
     */
    public void showTransactionView(MessageOutput output) {
        if (output == null) return;
        new PushMsgTransactionDialog(KxeApplication.getInstance())
                .timestamp(output.getPayTime())
                .amount(output.getPayAmount())
                .cashAmount(null)
                .currencyName(output.getCurrencyName())
                .targetView(R.layout.dialog_tran_push_msg)
                .show();
    }

    /**
     * 充值会员
     */
    public void showRechargeMemberView(MessageOutput output) {
        if (output == null) return;
        EventBus.getDefault().post(new RefreshUserEvent());
        new PushMsgUpgradeDialog(KxeApplication.getInstance())
                .title(output.getTitle())
                .content(output.getContent())
                .show();
    }

    /**
     * 公告弹框
     */
    public void showPushNoticeView(MessageOutput output) {
        if (output == null) return;
        new PushNoticeDialog(KxeApplication.getInstance())
                .title(output.getTitle())
                .content(output.getContent())
                .show();
    }

    /**
     * 认证
     */
    public void showAuthIdentityView(MessageOutput output) {
        if (output == null) return;
        EventBus.getDefault().post(new RefreshUserEvent());
        int icon = output.getAuthResult() ? R.drawable.img_success_result : R.drawable.img_fail_result;
        new PushMsgAuthenDialog(KxeApplication.getInstance())
                .title(output.getTitle())
                .content(output.getContent())
                .icon(icon)
                .show();
    }
}
